package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> elqw = new LambdaQueryWrapper<>();
        elqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(elqw);
        //如果根据用户名查询不到结果直接return
        if (emp == null) {
            return R.error("登录失败");
        }
        //获取密码并用MD5进行加密

        //如果查询到的对象的密码和接收的密码不匹配，直接返回
        if (!emp.getPassword().equals(password)) {
            return R.error("登录失败");
        }
        //如果查询用户的对象是已禁用状态，返回结果
        if (emp.getStatus() == 0) {
            return R.error("用户已禁用");
        }
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);


    }
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
    @PostMapping
    public R<String> add(HttpServletRequest request ,@RequestBody Employee employee){
        //前端界面的属性和数据库属性不一致，手动补齐属性在传输给后台
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //创建人及修改人属性，需要获取登录人的属性
        //登录人的属性从Session数据中获取
        Long emp =(Long) request.getSession(). getAttribute("employee");

        employee.setCreateUser(emp);
        employee.setUpdateUser(emp);*/
        employeeService.save(employee);
        return R.success("添加成功");
    }
    @GetMapping("/page")
    //根据前段界面，接收过来的参数不是JSON格式，可以通过int数据类型直接获取
    public R<Page> pageSelect(int page,int pageSize,String name){
        //分页构造器
        Page pageInfo = new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isEmpty(name),Employee::getName,name);
        //排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfo);
        return R.success(pageInfo);
    }
    @PutMapping
    public R<String> setEmplyee(HttpServletRequest request,@RequestBody Employee employee){
        /*employee.setUpdateTime(LocalDateTime.now());
        Long employee1 = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(employee1);*/
        employeeService.updateById(employee);

        return  R.success("员工信息更改成功");
    }
    @GetMapping("/{id}")
    public R getId(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee!=null){
            return R.success(employee);
        }
        return R.error("未查询到该用户");
    }

    @GetMapping
    public R update(HttpServletRequest request,@RequestBody Employee employee){
        /*Long employee1 = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(employee1);*/
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }
}